package nullby.omnia.fragment;

import java.util.ArrayList;

import nullby.common.utils.CustomLog;
import nullby.omnia.R;
import nullby.omnia.activity.voc.ActivityVocabulareWord;
import nullby.omnia.adapter.AdapterVocWord;
import nullby.omnia.model.WordOfVoc;
import nullby.omnia.ui.EditTextClear;
import nullby.omnia.ui.EditTextClear.Listener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class FragmentVocabulare extends Fragment {
	private static final String TAG = FragmentVocabulare.class.getSimpleName();

	// private static final String DB_NAME = "ruzh.whd";
	// private static final String TABLE_NAME = "whd_entries_content";
	private ListView listView;
	// private SQLiteDatabase database;
	private EditTextClear etClear;

	private AdapterVocWord adapter;

	private Animation animation1;

	public static FragmentVocabulare newInstance() {
		FragmentVocabulare fragment = new FragmentVocabulare();
		return fragment;
	}

	public FragmentVocabulare() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		CustomLog.logI(TAG, "onActivityCreated");
		super.onActivityCreated(savedInstanceState);

		listView = (ListView) getView().findViewById(R.id.lvVocabulare);

		listView.setEmptyView(getActivity().findViewById(R.id.empty));

		etClear = (EditTextClear) getView().findViewById(R.id.etClear);
		etClear.setOnOkClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				InputMethodManager imm = (InputMethodManager) getActivity()
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(etClear.getWindowToken(), 0);
			}
		});

		etClear.setListener(new Listener() {

			@Override
			public void onComplete(ArrayList<WordOfVoc> list) {
				adapter = new AdapterVocWord(getActivity(), R.id.tvWord, list);
				listView.setAdapter(adapter);
				adapter.notifyDataSetChanged();

				listView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> adapter, View v,
							int id, long arg3) {
						Intent intent = new Intent(getActivity(),
								ActivityVocabulareWord.class);
						intent.putExtra("idCard", (String) v.getTag());
						startActivity(intent);

					}
				});
			}

		});

		animation1 = AnimationUtils.loadAnimation(getActivity(),
				R.anim.top_bottom);
		etClear.startAnimation(animation1);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		CustomLog.logI(TAG, "onCreateView");
		return inflater.inflate(R.layout.fragment_voc, container, false);
	}

	// @Override
	// public void onClick(View v) {
	//
	// }

}
